package com.itheima.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletContextDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ServletContextDemo");
        // 获取ServletContext对象
        ServletContext context = getServletContext();
        // 获取全局配置参数
        String name = context.getInitParameter("name");
        System.out.println(name);
        // 获取文件的虚拟路径
        String path = context.getContextPath();
        System.out.println(path);
        // 获取文件的虚拟目录的绝对路径
        String realPath = context.getRealPath("/");
        System.out.println(realPath);///Users/ronglei/Desktop/ronglei/study/javaWeb/java_web/out/artifacts/java_web_war_exploded/
        //设置共享数据
        context.setAttribute("name", "zhangsan");
        //获取共享数据
        //Object name1 = context.getAttribute("name");
        //System.out.println(name1);
        //删除共享数据
        //context.removeAttribute("name");


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
